<?php

/**
 * Search Model
 *
 */
class Search_Model_Search
{
    const RESOURCE_NAME='search';
    const UPLOAD_DIR='data/index';
    
    protected $_index;
    
    public function __construct()
    {
        Zend_Search_Lucene_Analysis_Analyzer::setDefault(
            new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive()
        );
        
        Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
        
        try {
            $this->_index = Zend_Search_Lucene::open(ROOT_DIR.'/'.self::UPLOAD_DIR);
         } catch( Exception $e) {
            $this->_index = Zend_Search_Lucene::create(ROOT_DIR.'/'.self::UPLOAD_DIR);
        }
    }
    
    /**
     * Creates node index
     * 
     * @param int $id
     * @param string $url
     * @param string $title
     * @param string $text
     * @param string $language
     */
    public function index($id,$url,$title,$text,$meta_keywords='',$meta_description='',$language=null)
    {
        if (empty($language)) $language=Zend_Registry::get('language')->getLocale();
        
        $doc = new Zend_Search_Lucene_Document();
        $doc->addField(Zend_Search_Lucene_Field::Text('content', $text, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('node_id', $id, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('url', $url, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('title', $title, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('meta_keywords', $meta_keywords, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('meta_description', $meta_description, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::Text('language', $language, 'utf-8'));
        
        $this->_index->addDocument($doc);
        
        $this->_index->commit();                                   
    }
    
    /**
     * Optimizes index
     * 
     */
    public function optimize()
    {
        $this->_index->optimize();                                   
    }
    
    /**
     * Clear exisiting index docs
     * 
     */
    public function clear()
    {
        for ($count = 0; $count < $this->_index->maxDoc(); $count++) {
            if (!$this->_index->isDeleted($count)) {
                $this->_index->delete($count);
            }
        }
    }
    
    /**
     * Searches query in index
     * 
     * @param string $query
     * 
     * @return array
     */
    public function find($query)
    {
        $result=array();
        
        $hits = $this->_index->find($query);
        
        foreach ($hits as $hit) {
            if ($hit->language != Zend_Registry::get('language')->getLocale()) continue;
            
            $result[]=array(
                        'id' => $hit->node_id,
                        'url'  => $hit->url,
                        'title' => $hit->title,
                        'content' => $hit->content,
                        'language' => $hit->language,
                        'meta_keywords' => $hit->meta_description,
                        'meta_description' => $hit->meta_description
                        );
        }
        
        return $result;
    }
    
    /**
     * Returns search results page
     * 
     * @param array $results
     * @param int $page
     * @param int $limit
     * 
     * @return object
     */
    public function fetch(&$results,$page=1,$limit=10)
    {
        $adapter=new Zend_Paginator_Adapter_Array($results);
        
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber((int) $page);
        $paginator->setItemCountPerPage($limit);
        
        return $paginator;
    }
}
